using System;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Collections.Generic;

namespace ServStop
{
      public static class FileSerializer
      {

          private static string _currentlyLoadedUserList;

          public static void Serialize( string filename, object objectToSerialize )
          {
              if(objectToSerialize == null)
                  throw new ArgumentNullException( "objectToSerialize cannot be null" );

              Stream stream = null;

              try
              {
                  stream = File.Open( filename, FileMode.Create );
                  BinaryFormatter bFormatter = new BinaryFormatter();
                  bFormatter.Serialize(stream, objectToSerialize);
              }
              finally
              {
                  if (stream != null)
                  stream.Close();
              }

              CurrentlyLoadedUserList = filename;
      }

      public static string CurrentlyLoadedUserList
      {
            get{ return _currentlyLoadedUserList; }
            private set{ _currentlyLoadedUserList = value; }

      }

      public static T Deserialize <T>( string filename )
      {
              T objectToSerialize;

              Stream stream = null;

              try
              {
                  stream = File.Open( filename, FileMode.Open );
                  BinaryFormatter bFormatter = new BinaryFormatter();
                  objectToSerialize = (T)bFormatter.Deserialize(stream);
              }
              finally
              {
                  if (stream != null)
                      stream.Close();
              }

              CurrentlyLoadedUserList = filename;

              return objectToSerialize;
       }
    }
}
